import { Logger } from "../../engine/Logger";
import { RoomPlayer } from "./RoomPlayer";

export class GameResult {

    public static instance: GameResult

    public id: number
    public gameConf: object;
    public gameType: number
    public playerList: Array<RoomPlayer> = []
    public room_id: number;
    public clubid: number;
    public time: number;
    public bigWin: number;

    public static json2Model(data: object) {
        let gameResult: GameResult = new GameResult()
        gameResult.id = data["id"]
        gameResult.gameType = data["gameType"]
        gameResult.gameConf = data["gameConf"]
        let playerObjList: Array<object> = data["playerList"]
        gameResult.initPlayerList(playerObjList);
        gameResult.room_id = data["room_id"]
        gameResult.clubid = data["clubid"]
        gameResult.time = data["time"]
        gameResult.bigWin = data["bigWin"]
     
        
        return gameResult;
    }

    public static db2Model(data: object) {
        let gameResult: GameResult = new GameResult()
        Logger.log("db2Model==", data)
        gameResult.id = data["id"]
        gameResult.gameType = data["gameType"]
        let gameConfig: object = JSON.parse(data["gameConf"])
        gameResult.gameConf = gameConfig
        let playerObjList: Array<object> = JSON.parse(data["playerList"])
        gameResult.initPlayerList(playerObjList);
        gameResult.room_id = data["room_id"]
        gameResult.clubid = data["clubid"]
        gameResult.time = new Date(data["time"]).getTime()
        gameResult.bigWin = data["bigWin"]

        return gameResult;
    }

    public initPlayerList(playerList: Array<object>) {
        for (let i = 0; i < playerList.length; i++) {
            if (playerList[i]) {
                let player: RoomPlayer = RoomPlayer.createPlayerByObj(playerList[i])
                this.playerList[i] = player;
            } else {
                this.playerList[i] = null
            }
        }
    }

}